package com.my.study.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @author Carlos
 * @version 1.0
 * @Description 多数据源事务配置
 * @date 2021/11/7 12:06
 **/
@Configuration
@ComponentScan(value = "com.my.study")
@EnableTransactionManagement
public class MultiTxConfig {

    // ================= 第一个数据源配置 ================= //

    @Primary
    @Bean("druidDataSourceOne")
    public DruidDataSource druidDataSourceOne() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/mybaiscode?serverTimezone=UTC");
        druidDataSource.setUsername("ydj");
        druidDataSource.setPassword("ydjws");
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return druidDataSource;
    }

    @Primary
    @Bean("jdbcTemplateOne")
    public JdbcTemplate jdbcTemplateOne(@Qualifier("druidDataSourceOne") DataSource dataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

    @Primary
    @Bean("transactionManagerOne")
    public PlatformTransactionManager dataSourceTransactionManagerOne(@Qualifier("druidDataSourceOne") DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }

    // ================= 第二个数据源配置 ================= //

    @Bean("druidDataSourceTwo")
    public DruidDataSource druidDataSourceTwo() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/books?serverTimezone=UTC");
        druidDataSource.setUsername("ydj");
        druidDataSource.setPassword("ydjws");
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return druidDataSource;
    }

    @Bean("jdbcTemplateTwo")
    public JdbcTemplate jdbcTemplateTwo(@Qualifier("druidDataSourceTwo") DataSource dataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

    @Bean("transactionManagerTwo")
    public PlatformTransactionManager dataSourceTransactionManagerTwo(@Qualifier("druidDataSourceTwo") DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }


}
